package com.shuma.product.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.shuma.common.vo.R;
import com.shuma.product.service.GoodsDetailInfoService;
import com.shuma.product.service.impl.GoodsDetailInfoServiceImpl;
import com.shuma.product.vo.req.GoodsBaseAndDetail;
import com.shuma.product.pojo.GoodsBaseInfo;
import com.shuma.product.pojo.GoodsDetailInfo;
import com.shuma.product.vo.req.SelectQueryGoodsBase;
import com.shuma.product.service.GoodsBaseInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.shuma.product.ExceptionEnum.ExceptionEnum.PARAM_TYPE_BIND_ERROR;
import static com.shuma.product.ExceptionEnum.ExceptionEnum.REQUESTPARAM_FAIL;

@RestController
@CrossOrigin
public class GoodsBaseInfoController {
    @Resource
    GoodsBaseInfoService goodsBaseInfoService;

    //分页查询所有基础商品并带有筛选条件
    @GetMapping("/queryAllbasegoods")
    public R queryAllbasegoodsRequest(@RequestBody SelectQueryGoodsBase selectQueryGoodsBase){
        //有的数据不能为空，字符串只能为空字符串“” 必须要有当前用户id，当前第几页，每页大小。
        if(selectQueryGoodsBase.getOperatorname()==null||
                selectQueryGoodsBase.getGoodsname()==null||
                selectQueryGoodsBase.getLoginId()==null||
                selectQueryGoodsBase.getPageNum()==null||
                selectQueryGoodsBase.getPageSize()==null)
            return R.fail(PARAM_TYPE_BIND_ERROR.getCode(),PARAM_TYPE_BIND_ERROR.getMessage());
        return goodsBaseInfoService.getgoodsByoperatorid(selectQueryGoodsBase.getLoginId(),
                selectQueryGoodsBase.getPageNum(),
                selectQueryGoodsBase.getPageSize(),
                selectQueryGoodsBase.getGoodsonlinestatus(),
                selectQueryGoodsBase.getGoodsbaseid(),
                selectQueryGoodsBase.getGoodsname(),
                selectQueryGoodsBase.getOnlinetime(),
                selectQueryGoodsBase.getExpiretime(),
                selectQueryGoodsBase.getOperatorname(),
                selectQueryGoodsBase.getRole());
    }
    //查询单个商品全部信息
    @GetMapping("/queryOnegoods")
    public R queryOnegoodsRequest(@RequestParam("goodsbaseId") Long goodsbaseId) throws InterruptedException {
        return goodsBaseInfoService.getgoodsBybaseGoodsid(goodsbaseId);
    }
    //添加商品和草稿
    @PostMapping("/addOnegoods")
    public R addOnegoodsRequest(@RequestBody GoodsBaseAndDetail goodsBaseAndDetail) throws InterruptedException {
        GoodsBaseInfo goodsBaseInfo = goodsBaseAndDetail.getGoodsBaseInfo();
        GoodsDetailInfo goodsDetailInfo = goodsBaseAndDetail.getGoodsDetailInfo();
        if(goodsBaseInfo.getIsdelete().equals(1)){
            return R.fail(REQUESTPARAM_FAIL.getCode(),REQUESTPARAM_FAIL.getMessage());
        }
        Snowflake snowflake = IdUtil.getSnowflake();
        Long id = snowflake.nextId();
        goodsBaseInfo.setGoodsBaseId(id);
        goodsDetailInfo.setGoodsDetailId(id);
        return goodsBaseInfoService.addBaseDetailGood(goodsBaseInfo, goodsDetailInfo);
    }
    //上下线商品
    @PostMapping("/updateonlineBaseGoods")
    public R updateonlineBaseGoodsRequest(@RequestParam("goodsbaseId") Long goodsbaseId,@RequestParam("onlinestatus") Integer onlinestatus) throws InterruptedException {
        return goodsBaseInfoService.updateonlineBaseGood(goodsbaseId,onlinestatus);
    }
    //修改商品和修改商品草稿
    @PostMapping("/updateOnegoods")
    public R updateOnegoodsRequest(@RequestBody GoodsBaseAndDetail goodsBaseAndDetail) throws InterruptedException {
        GoodsBaseInfo goodsBaseInfo = goodsBaseAndDetail.getGoodsBaseInfo();
        GoodsDetailInfo goodsDetailInfo = goodsBaseAndDetail.getGoodsDetailInfo();
        return goodsBaseInfoService.updateBaseDetailGood(goodsBaseInfo, goodsDetailInfo);
    }
    //删除商品
    @PostMapping("/deteleOnegoods")
    public R deteleOnegoodsRequest(@RequestParam("goodsbaseId") Long goodsbaseId) throws InterruptedException {
        return goodsBaseInfoService.deleteBaseDetailGood(goodsbaseId);
    }
}
